//
//  BackupsManager.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/27.
//

import Foundation


enum BackupsType:String , CaseIterable, Identifiable {
    var id: String {
        self.rawValue
    }
    case local = "local"
    case ali = "ali"
    case cloud = "cloud"
}


//备份的
class BackupsManager: ObservableObject {
    static let shared = BackupsManager()
    private var delegateConfig : ConfigProtocol? = nil
    private var delegateDiary : DiaryProtocol? = nil
    private var delegateFile : FileProtocol? = nil
    
    
    private let userDefaults = UserDefaults.standard
    private let backupsKey = "backupsKey"
    private let diaryQueue = DispatchQueue(label: "diaryQueue")
    private let configQueue = DispatchQueue(label: "configQueue")
    private let fileQueue = DispatchQueue(label: "fileQueue")

    init() {
      
    }
    
    func doRead() {
        //读取存储的备份模式
        if let typeValue = userDefaults.string(forKey: backupsKey) {
            //string转换为枚举
            if let restoredBackupsType = BackupsType(rawValue: typeValue) {
                //审核版本使用网络备份
                backupsType = isExamine() ? BackupsType.cloud : restoredBackupsType
            }
        }
    }
    
    @Published var backupsType: BackupsType = BackupsType.local { // 提供一个初始值
        didSet {
            reset()
            userDefaults.set(backupsType.rawValue, forKey: backupsKey) // 存储原始值字符串
          
        }
    }
    
    //是否相同备份
    func isSameBackups(_ type:BackupsType) -> Bool {
        return backupsType == type
    }
    //是否本地备份
    func isLocalBackups() -> Bool {
        return backupsType == BackupsType.local
    }
    
    func setAccount() {
        showLog("设置账号")
        switch backupsType {
        case BackupsType.local:
            PushExt.setAccount(account: nil)
        case BackupsType.cloud:
            PushExt.setAccount(account: userManager.getUserId())
        case BackupsType.ali:
            PushExt.setAccount(account: nil) //暂时没有
        }
    }
    
    //备份图标
    func getBackupsIcon() -> ImageResource{
        switch backupsType {
        case BackupsType.local:
            return .homeIconBackupsLocal
        case BackupsType.cloud:
            return .homeIconBackupsQiniu
        case BackupsType.ali:
            return .homeIconBackupsAli
        }
    }
    
    //备份名称
    func getBackupsName() -> String{
        switch backupsType {
        case BackupsType.local:
            return "本地储存"
        case BackupsType.cloud:
            return "官方储存"
        case BackupsType.ali:
            return "阿里云盘储存"
        }
    }
    
    
    //配置信息
    func getDelegateConfig()-> ConfigProtocol? {
        return configQueue.sync {
            if delegateConfig == nil {
                delegateConfig = getDelegateConfig(type : backupsType)
            }
            return delegateConfig
        }
    }
    
    //配置信息
    func getDelegateConfig(type: BackupsType)-> ConfigProtocol? {
        switch type {
        case BackupsType.local:
            return ConfigDelegate(instance: LocalConfigImpl())
        case BackupsType.cloud:
            return ConfigDelegate(instance: CloudConfigImpl())
        case BackupsType.ali:
            return ConfigDelegate(instance: AliConfigImpl())
        }
    }
    
    //文件信息
    func getDelegateFile()-> FileProtocol? {
        return fileQueue.sync {
            if delegateFile == nil {
                delegateFile = getDelegateFile(type : backupsType)
            }
            return delegateFile
        }
    }
    
    //文件信息
    func getDelegateFile(type: BackupsType)-> FileProtocol? {
        switch type {
        case BackupsType.local:
            return FileDelegate(instance: LocalFileImpl())
        case BackupsType.cloud:
            return FileDelegate(instance: CloudFileImpl())
        case BackupsType.ali:
            return FileDelegate(instance: AliFileImpl())
        }
    }
    

    //日记
    func getDelegateDiary() -> DiaryProtocol? {
        // 使用串行队列保证线程安全
        return diaryQueue.sync {
            if delegateDiary == nil {
                delegateDiary = getDelegateDiary(type: backupsType)
                
                // 2. 增加对初始化失败的处理
                if delegateDiary == nil {
                    showLog("警告：getDelegateDiary(type:) 返回 nil，backupsType: \(backupsType)")
                }
            }
            return delegateDiary
        }
    }
    
    //文件信息
    func getDelegateDiary(type: BackupsType)-> DiaryProtocol? {
        switch type {
        case .local:
            return DiaryDelegate(instance: LocalDiaryImpl())
        case .cloud:
            return DiaryDelegate(instance: CloudDiaryImpl())
        case .ali:
            return DiaryDelegate(instance: AliDiaryImpl())
        }
    }
    
    func reset() {
        delegateConfig = nil
        delegateFile = nil
        delegateDiary = nil
    }
    
}

